package com.leyou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.dto.SpecParamDTO;
import com.leyou.entity.SpecParam;
import com.leyou.mapper.SpecParamMapper;
import com.leyou.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

//TODO 规格参数实现类
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam>
        implements SpecParamService {
    @Autowired
    private SpecParamService specParamService;

    @Override
    public List<SpecParamDTO> listSpecParam(Long cid, Boolean searching, Long gid) {
        //代码健壮性判断
        if (cid==null&&gid==null){
            //条件不能为空
            throw new LyException(400,"查询条件不能为空");
        }
        //查询
        List<SpecParam> list=query()
                .eq(cid!=null,"category_id",cid)
                .eq(searching!=null,"searching",searching)
                .eq(gid!=null,"group_id",gid).list();
        return SpecParamDTO.convertEntityList(list);
    }

    //todo 新增规格参数
    @Override
    public void saveSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        this.save(specParam);
    }

    //todo 修改规格参数
    @Override
    public void updateParam(SpecParamDTO specParamDTO) {
        this.save(specParamDTO.toEntity(SpecParam.class));
    }
}
